﻿using System.Windows;
using System.Windows.Controls;


namespace Lib.WPF.Core;
/// <summary>
/// ViewModel基类
/// </summary>
public class ViewModelBase : PropertyChangedBase {
    /// <summary>
    /// 构造函数
    /// </summary>
    public ViewModelBase() {
        this.Window = Application.Current.MainWindow;
        this.Window.Loaded += (Sender, e) => {
            Initialize();
        };
    }
    /// <summary>
    /// 界面加载完成后初始化（派生类中要重写Override）
    /// </summary>
    public virtual void Initialize() { }


    /// <summary>
    /// 查找具有提供的标识符名的元素
    /// </summary>
    public T Find<T>(string Name) {
        if (string.IsNullOrEmpty(Name)) {
            return default;
        }
        var Element = Window.FindName(Name);
        if (Element == null) {
            return default;
        }
        return (T)Element;
    }
    /// <summary>
    /// 当前窗口对象
    /// </summary>
    public System.Windows.Window Window { get; set; }

    /// <summary>
    /// 获取指定资源数据
    /// </summary>
    public static ResourceDictionary Resources { get; set; } = Application.Current.Resources;
    /// <summary>
    /// 获取指定资源数据
    /// </summary>
    public static T GetResource<T>(string Key) {
        if (string.IsNullOrEmpty(Key) || !Resources.Contains(Key)) {
            return default;
        }
        return (T)Resources[Key];
    }
    /// <summary>
    /// 数字字典
    /// </summary>
    public static Dictionary<string, double> Digits { get; set; } = [];
    /// <summary>
    /// 页面字典
    /// </summary>
    public static Dictionary<string, Page> Pages { get; set; } = [];
}